######################################################################################
#
# This file was provided as part of the SharePoint Mystery Deployment package
# Created by Christopher Kimbell (http://sharepointmystery.com)
#
######################################################################################

# -----------------------------------------------------------------
# Creates an object used to manipulate a database
# -----------------------------------------------------------------
function New-Database()
{
    $obj=new-object PSObject -Property @{
        Server = "";
		FailoverServer = "";
        Name = "";
        FilePath = "";
        FileSize = "1000MB";
        LogPath = "";
        LogSize = "100MB";
        RecoveryModel = "FULL";
    }
	Add-Property $obj "FullName" `
	{
		return "$($farm.Prefix)$($this.Name)"
	}
	
    # -----------------------------------------------------------------
    # Validates the information in the object
    # -----------------------------------------------------------------
    Add-Method $obj "Validate" `
    {
        Write-Operation "    Database"  
        Validate-NotEmpty "Server" $this.Server
        Validate-NotEmpty "Name" $this.Name
        Validate-NotEmpty "FilePath" $this.FilePath
        Validate-NotEmpty "FileSize" $this.FileSize
        Validate-NotEmpty "LogPath" $this.LogPath
        Validate-NotEmpty "LogSize" $this.LogSize
        Validate-MemberOf "RecoveryModel" $this.RecoveryModel "SIMPLE","FULL"
    }
    # -----------------------------------------------------------------
    # Provisions the database
    # -----------------------------------------------------------------
    Add-Method $obj "Provision" `
    {
        #param($cred)
        
        if($farm.CreateDatabaseFolders -eq $true)
        {
            New-SPMServerDirectory -Path $this.FilePath -Servers $farm.Servers
            New-SPMServerDirectory -Path $this.LogPath -Servers $farm.Servers
        }
    
        if($farm.CreateDatabases -eq $true)
        {
            $loc = Get-Location
            
            $cred = $farm.ResolveCredentials()
            
            #if($cred -eq $null)
            #{
            #    Run-SPMDatabaseScript -DataSource $this.Server `
            #        -InitialCatalog "MASTER" `
            #        -ScriptFile ($loc.Path + "\CreateDatabase.sql") `
            #        -Tokens "DatabaseName=$($this.FullName);FilePath=$($this.FilePath);LogPath=$($this.LogPath);FileSize=$($this.FileSize);LogSize=$($this.LogSize);RecoveryMode=$($this.RecoveryModel)"
            #}
            #else
            #{
                Write-Operation "Creating Database $($this.FullName) using $($cred.UserName)" 
                $sb = 
                {   
                    param($loc, $server, $name, $filePath, $logPath, $fileSize, $logSize, $recoveryModel)      
                    Add-PSSnapin Microsoft.SharePoint.PowerShell          
                    
					$logFile = $loc + "\_" + $name + ".log"
					Start-Transcript $logFile
					
                    Run-SPMDatabaseScript -DataSource $server `
                        -InitialCatalog "MASTER" `
                        -ScriptFile ($loc.Path + "\CreateDatabase.sql") `
                        -Tokens "DatabaseName=$($name);FilePath=$($filePath);LogPath=$($logPath);FileSize=$($fileSize);LogSize=$($logSize);RecoveryMode=$($recoveryModel)"
                
					Stop-Transcript
				} 
            
                $job = Start-Job -Credential $cred `
                                 -ScriptBlock $sb `
                                 -ArgumentList $loc, $this.Server, $this.FullName, $this.FilePath, $this.LogPath, $this.FileSize, $this.LogSize, $this.RecoveryModel
                Wait-Job $job
				
            #}
        }
    }
    # -----------------------------------------------------------------
    # Adds a role to the database
    # -----------------------------------------------------------------
    Add-Method $obj "AddRole" `
    {
        param ($login, $role)
       
        Run-SPMDatabaseScript -DataSource $this.Server `
            -InitialCatalog "MASTER" `
            -ScriptFile ((Get-Location).Path + "\AddDatabaseRole.sql") `
            -Tokens "DatabaseName=$($farm.Prefix)$($this.Name);Login=$($login);Role=$($role)"
    }
    return $obj
}
